import {
  login,
  getUserInfo,
  tiedUser,
  logout,
  getOrdersList
} from '../../request/api'
import Toast from '@vant/weapp/toast/toast';
import utils from '../../utils/socketTask'
Page({
  data: {
    showOrders: false,
    token: "",
    userInfo: {},
    loading: false,
    saveLoading: false,
    studentNumber: null,
    ordersList: [],
  },
  userLogin(e) {
    let that = this
    wx.login({
      success: (res) => {
        this.setData({
          loading: true
        })
        console.log(res.code);
        let obj = {
          code: res.code
        }
        Toast.loading({
          message: '登录中...',
          forbidClick: true,
          loadingType: 'spinner'
        })
        login(obj).then(res => {
          if (res.code == 200) {
            Toast({
              type: 'success',
              message: '登录成功',
              duration: 3000
            })
            console.log('登录', res)
            setTimeout(() => {
              wx.setStorageSync('token', res.data)
              wx
              that.setData({
                token: wx.getStorageSync('token'),
                loading: false
              })
              that.getUserInfoData()
            }, 2000)
          } else {
            Toast.fail('登陆失败')
          }
        })
      },
    })
  },
  getUserInfoData() {
    getUserInfo().then(res => {
      if (res.code == 200) {
        console.log('用户信息', res)
        wx.setStorageSync('studentNumber', res.data.studentNumber)
        this.setData({
          token: wx.getStorageSync('token'),
          userInfo: res.data,
          studentNumber: wx.getStorageSync('studentNumber')
        })
      }

    })
  },
  tiedUserInfoData(e) {
    if (e.detail.value.studentNumber == "") {
      Toast({
        type: 'fail',
        message: '请填写学号',
        duration: 3000
      })
      console.log(e.detail.value.studentNumber);
      return
    } else if (!(/(^[0-9]*$)/.test(e.detail.value.studentNumber))) {
      Toast({
        type: 'fail',
        message: '格式错误',
        duration: 3000
      })
      return
    } else {
      let query = {
        studentNumber: e.detail.value.studentNumber
      }
      tiedUser(query).then(res => {
        if (res.code == 200) {
          console.log('修改学生信息', res)
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          Toast({
            type: 'fail',
            message: '学号错误',
            duration: 3000
          })
        }
      })
      this.getUserInfoData()
    }

  },
  userLogout() {
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success(res) {
        if (res.confirm) {
          that.setData({
            loading: true
          })
          logout().then(res => {
            if (res.code == 200) {
              console.log('退出登录', res)
              Toast({
                type: 'success',
                message: '退出登录成功',
                duration: 3000
              })
              setTimeout(() => {
                wx.removeStorageSync('token')
                that.setData({
                  token: wx.getStorageSync('token'),
                  loading: false
                })
              }, 2000)

            }
          })
        } else if (res.cancel) {}
      }
    })
  },
  showOrdersList() {
    this.getOrdersListData()
    this.setData({
      showOrders: true
    })
  },
  closeOrdersList() {
    this.setData({
      showOrders: false
    })
  },
  getOrdersListData() {
    getOrdersList().then(res => {
      if (res.code == 200) {
        console.log('订单列表', res)
        res.data.forEach(value => {
          value.time = new Date(+new Date(value.ordersTime) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
        })
        this.setData({
          ordersList: res.data
        })

      }
    })
  },
  onLoad(options) {
    this.getUserInfoData()
    utils.socketTask()
  },

  onReady() {},

  onShow() {},

  onHide() {},

  onUnload() {},

  onPullDownRefresh() {},

  onReachBottom() {},

  onShareAppMessage() {}
})